package com.platform.modules.cpt.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.util.StringUtil;
import com.platform.common.utils.RestResponse;
import com.platform.modules.cpt.entity.CptAcceptanceEntity;
import com.platform.modules.cpt.entity.CptAcceptanceSuperviseReportStampEntity;
import com.platform.modules.cpt.service.CptAcceptanceSuperviseReportStampService;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysUserEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author biaohuaming
 * @date 2020-02-24 15:28:20
 */
@Api(tags = "工程质量监督报告审批盖章")
@RestController
@RequestMapping("cpt/acceptance/superviseReportStamp")
public class CptAcceptanceSuperviseReportStampController extends AbstractController {


    @Autowired
    private CptAcceptanceSuperviseReportStampService cptAcceptanceSuperviseReportStampService;



    /**
     * @descr 查询<br>
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/view")
    public RestResponse view(@RequestParam String cptAcceptanceId) {
    	Map<String, Object> params = getParams();
    	params.put("cptId", cptAcceptanceId);
    	CptAcceptanceSuperviseReportStampEntity entity = cptAcceptanceSuperviseReportStampService.queryOne(params);
        return RestResponse.success().put("data", entity);
    } 
    
    /**
     * @param cptAcceptanceSuperviseReportStampEntity
     * @return
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public CommonResult<CptAcceptanceSuperviseReportStampEntity> save(@RequestBody CptAcceptanceSuperviseReportStampEntity cptAcceptanceSuperviseReportStampEntity) {
    	SysUserEntity sysUserEntity = this.getUser();
    	if(StringUtil.isEmpty(cptAcceptanceSuperviseReportStampEntity.getId())) {
    		cptAcceptanceSuperviseReportStampEntity.setCreateTime(new Date());
    		cptAcceptanceSuperviseReportStampEntity.setCreateUser(sysUserEntity.getUserId());
    	}else {
    		cptAcceptanceSuperviseReportStampEntity.setUpdateTime(new Date());
    		cptAcceptanceSuperviseReportStampEntity.setUpdateUser(sysUserEntity.getUserId());
    	}
    	cptAcceptanceSuperviseReportStampService.saveOrUpdate(cptAcceptanceSuperviseReportStampEntity);
        return CommonResult.success(cptAcceptanceSuperviseReportStampEntity);
    }
    
    /**
     * 生成 工程质量监督报告(后续盖章)
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "生成-工程质量监督报告(后续盖章)")
    @PostMapping("/gczljdbg1File/create")
    public CommonResult createGczljdbg1File(@RequestBody CptAcceptanceEntity cptAcceptanceEntity) {
    	Map<String, Object> result = cptAcceptanceSuperviseReportStampService.createGczljdbg1File(cptAcceptanceEntity, "工程质量监督报告.pdf");
        return CommonResult.success(result);
    }
    
    @ApiOperation(value = "下载-工程质量监督报告 文件流")
    @GetMapping("/gczljdbg1File/download")
    public void downloadGczljdbg1File(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceSuperviseReportStampService.downloadGczljdbgFile(cptId, request, response, "CPT_ACCPTANCE_FILES_GCZLJDBG1");
    }
    
    @ApiOperation(value = "下载-工程质量监督报告 文件")
    @GetMapping("/gczljdbg1File/sign/download")
    public void downloadSignGczljdbg1File(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceSuperviseReportStampService.downloadSignGczljdbgFile(cptId, request, response, "CPT_ACCPTANCE_FILES_GCZLJDBG1");
    }
    

    @ApiOperation(value = "盖章-工程质量监督报告 文件")
    @PostMapping("/gczljdbg1File/sign")
    public RestResponse signGczljdbg1File(HttpServletRequest request,MultipartFile file, 
    		@RequestParam String cptId, 
    		@RequestParam String attId,
    		@RequestParam(required=false) String fileName) {
    	
    	//获取已签章的合同
        String contentType = request.getContentType();
        
        if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
        	cptAcceptanceSuperviseReportStampService.signGczljdbgFile(request, cptId, "CPT_ACCPTANCE_FILES_GCZLJDBG1", "工程质量监督报告.pdf");
        }else {
        	RestResponse.error();
        }
        return RestResponse.success();
    }

}
